﻿using Microsoft.Practices.Prism.Commands;
using System;
using System.Collections.Generic;
using System.Text;
using dwindleUrl.Repositories;
using Microsoft.Practices.Prism.Mvvm;

namespace dwindleUrl.ViewModels
{
    public class DwindlePageViewModel :ViewModel,IDwindleViewModel
    {
        private Repositories.IShortenerRepository _shortenerRepository;

        public DwindlePageViewModel(IShortenerRepository shortenerRepository)
        {
            _shortenerRepository = shortenerRepository;
        }

        private string _url = default(string);
        public string Url { get { return _url; } set { SetProperty(ref _url, value); } }

        private Models.UrlCode _urlCode = default(Models.UrlCode);
        public Models.UrlCode UrlCode { get { return _urlCode; } set { SetProperty(ref _urlCode, value); } }

        public DelegateCommand AboutCommand { get; set; }

        private DelegateCommand _shortenCommand = null;
        public DelegateCommand ShortenCommand
        {
            get
            {
                if (_shortenCommand != null)
                    return _shortenCommand;
                _shortenCommand = new DelegateCommand
                    (
                    async () => { UrlCode = await _shortenerRepository.Shorten(Url); },
                    () => { return (this.Url != null && this.Url !=""); }
                    );
                this.PropertyChanged += (s, e) => _shortenCommand.RaiseCanExecuteChanged();
                return _shortenCommand;
            }
        }
    }
}
